﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace SpecialCharCheck
{
    public partial class Form1 : Form
    {
        private static Dictionary<string, string> LoadInvalidXmlChars()
        {
            Dictionary<string, string> invalidXmlChars = new Dictionary<string, string>();

            XmlDocument document = new XmlDocument();

            document.Load(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "InvalidXmlChars.xml"));

            XmlNodeList nodeList = document.SelectNodes("//InvalidXmlChar");

            if (nodeList != null)
            {
                foreach (XmlNode node in nodeList)
                {
                    string invalidChar = node.Attributes["Char"].Value;

                    if (string.IsNullOrEmpty(invalidChar))
                    {
                        invalidChar = new string(Convert.ToChar(int.Parse(node.Attributes["Ascii"].Value)), 1);
                    }

                    if (!invalidXmlChars.ContainsKey(invalidChar))
                        invalidXmlChars.Add(invalidChar, node.Attributes["Replacement"].Value);
                    //lower case
                    if (invalidChar.ToLower() != invalidChar && !invalidXmlChars.ContainsKey(invalidChar.ToLower()))
                    {
                        invalidXmlChars.Add(invalidChar.ToLower(), node.Attributes["Replacement"].Value.ToLower());
                    }
                    //upper case
                    if (invalidChar.ToUpper() != invalidChar && !invalidXmlChars.ContainsKey(invalidChar.ToUpper()))
                    {
                        invalidXmlChars.Add(invalidChar.ToUpper(), node.Attributes["Replacement"].Value.ToUpper());
                    }
                }
            }

            return invalidXmlChars;
        }

        Dictionary<string, string> invalidXmlChars = LoadInvalidXmlChars();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {

        }

        private void ShowFile(string file)
        {
            XmlDocument xmlDocument=new XmlDocument();
            xmlDocument.Load(file);

            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("Invalid Chars in File:\r\n\r\n");

            foreach (KeyValuePair<string, string> invalidXmlChar in invalidXmlChars)
            {
                if (xmlDocument.InnerXml.Contains(invalidXmlChar.Key))
                sb.AppendFormat("Char:{0}\r\n", invalidXmlChar.Key);
            }

            textBox1.Text = sb.ToString();
        }

        private int GetAscii(string key)
        {
            return (int) key[0];
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog { Multiselect = false };
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                ShowFile(openFileDialog.FileName);
            }
        }
    }
}
